package com.ruoyi.generator.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.generator.domain.TemplateEntity;
import com.ruoyi.generator.service.ITemplateService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 模板管理
 *
 * @author sparkstu
 * @date 2024-01-13
 */
@RestController
@RequestMapping("/generator/template")
public class TemplateController extends BaseController {
    @Autowired
    private ITemplateService templateService;

    /**
     * 查询模板管理列表
     */
    @PreAuthorize("@ss.hasPermi('generator:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(TemplateEntity template) {
        startPage();
        List<TemplateEntity> list = templateService.selectTemplateList(template);
        return getDataTable(list);
    }

    /**
     * 导出模板管理列表
     */
    @PreAuthorize("@ss.hasPermi('generator:template:export')")
    @Log(title = "template", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TemplateEntity template) {
        List<TemplateEntity> list = templateService.selectTemplateList(template);
        ExcelUtil<TemplateEntity> util = new ExcelUtil<TemplateEntity>(TemplateEntity.class);
        util.exportExcel(response, list, "template数据");
    }

    /**
     * 获取模板管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('generator:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(templateService.selectTemplateById(id));
    }

    /**
     * 新增模板管理
     */
    @PreAuthorize("@ss.hasPermi('generator:template:add')")
    @Log(title = "template", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TemplateEntity template) {
        return toAjax(templateService.insertTemplate(template));
    }

    /**
     * 修改模板管理
     */
    @PreAuthorize("@ss.hasPermi('generator:template:edit')")
    @Log(title = "template", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TemplateEntity template) {
        return toAjax(templateService.updateTemplate(template));
    }

    /**
     * 删除模板管理
     */
    @PreAuthorize("@ss.hasPermi('generator:template:remove')")
    @Log(title = "template", businessType = BusinessType.DELETE)
    @DeleteMapping
    public AjaxResult remove(@RequestBody List<Long> idList) {
        return toAjax(templateService.deleteTemplateByIds((Long[]) idList.toArray()));
    }
}